package com.roobo.demo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.roobo.mqttsdk.manager.CustomManager;
import com.roobo.mqttsdk.manager.ResultInterface;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * 发送自定义消息/设备事件/统计事件
 * 自定义消息：设备端和手机端相互发送自定义消息，消息格式是json对象，消息内容是设备端和手机端根据具体业务功能自定义
 * 设备事件：设备端给服务器发送事件，服务器接收到事件后，根据事件类型处理相关事件后给设备端返回相关结果，设备端根据事件类型和结果处理相关业务逻辑
 * 统计事件：设备端向服务器发送统计事件。比如按键事件。
 */


public class SendCustomActivity extends AppCompatActivity implements View.OnClickListener {

    public static final String TAG = SendCustomActivity.class.getSimpleName();

    private TextView mCustomMessageTv;
    private Button mSendCustomMessageBtn;
    private TextView mEventTv;
    private Button mSendEventBtn;
    private TextView mStatisticsTv;
    private TextView mSendStatisticsBtn;

    private String mCustomMessage;
    private String mEvent;
    private String mStatistics;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG, "onCreate");
        super.onCreate(savedInstanceState);
        setTitle(getResources().getString(R.string.send_custom_activity_title));
        setContentView(R.layout.activity_send_custom);
        initViews();
    }

    private void initViews() {
        mCustomMessageTv = findViewById(R.id.custom_message_tv);
        mSendCustomMessageBtn = findViewById(R.id.send_custom_message_btn);
        mEventTv = findViewById(R.id.event_tv);
        mSendEventBtn = findViewById(R.id.send_event_btn);
        mStatisticsTv = findViewById(R.id.statistics_tv);
        mSendStatisticsBtn = findViewById(R.id.send_statistics_btn);

        mSendCustomMessageBtn.setOnClickListener(this);
        mSendEventBtn.setOnClickListener(this);
        mSendStatisticsBtn.setOnClickListener(this);
        createCustomMessage();
        createEvent();
    }

    @Override
    public void onClick(View v) {
        String type;
        switch (v.getId()) {
            case R.id.send_custom_message_btn://发送自定义消息
                CustomManager.getInstance().sendCustomMessage(mCustomMessage, new ResultInterface() {
                    @Override
                    public void onResult(int code, String message) {
                        handleResult(code, message);
                    }
                });
                break;
            case R.id.send_event_btn://发送设备事件,查询设备绑定状态
                type = "event.boot";
                CustomManager.getInstance().sendEvent(type, mEvent, new ResultInterface() {
                    @Override
                    public void onResult(int code, String message) {
                        handleResult(code, message);
                    }
                });
                break;
        }
    }

    private void createCustomMessage() {
        JSONObject data = new JSONObject();//data为JSON字符串
        try {
            data.put("isChildLockOn", true);//键值自定义
            data.put("isEarLightOn", true);//键值自定义
        } catch (JSONException e) {
            e.printStackTrace();
            Log.d(TAG, "sendCustomMessage e.getLocalizedMessage:" + e.getLocalizedMessage());
        }
        mCustomMessage = data.toString();
        mCustomMessageTv.setText("自定义消息:" + data.toString());
    }

    private void createEvent() {
        JSONObject object = new JSONObject();
        try {
            object.put("bootState", 1);
        } catch (JSONException e) {
            e.printStackTrace();
            Log.d(TAG, "sendEvent e.getLocalizedMessage:" + e.getLocalizedMessage());
        }
        mEvent = object.toString();
        mEventTv.setText("设备事件(查询绑定状态):" + object.toString());
    }

    private void handleResult(int code, String message) {
        Log.d(TAG, "handleResult code：" + code + " message:" + message);
        if (code == CustomManager.SEND_SUCCESS) {
            Toast.makeText(this.getApplicationContext(), this.getApplicationContext().getResources().getString(R.string.send_success),
                    Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this.getApplicationContext(), this.getApplicationContext().getResources().getString(R.string.send_fail),
                    Toast.LENGTH_SHORT).show();
        }
    }
}
